package cn.ffcs.uec.outStaff.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.ListModelList;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffListMainBean;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfoVo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Controller
@Scope("prototype")
public class UecOutStaffListMainComposer extends BasePortletComposer {

	private static final long serialVersionUID = -2497274555244653652L;

	private UecOutStaffListMainBean bean = new UecOutStaffListMainBean();

	/**
	 * 外包人员基本信息
	 */
	private UecOutStaffInfo qryUecOutStaffInfo;
	/**
	 * 外包人员虚拟对象
	 */
	private UecOutStaffInfoVo uecOutStaffInfoVo;

	private UecOutStaffManager uecOutStaffManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");

	/**
	 * 操作类型
	 */
	private String opType;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化
	 */
	public void onCreate$uecOutStaffListMainWin() {
		this.bindBean();
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectRequest() throws Exception {
		if (this.bean.getUecOutStaffListbox().getSelectedCount() > 0) {
			uecOutStaffInfoVo = (UecOutStaffInfoVo) bean.getUecOutStaffListbox().getSelectedItem().getValue();
			if (uecOutStaffInfoVo != null) {
				/**
				 * 将获取到的虚拟对象转化为UecOutStaffInfo
				 */
				qryUecOutStaffInfo = UecOutStaffInfo.newInstance();
				qryUecOutStaffInfo.setOutStaffId(uecOutStaffInfoVo.getOutStaffId());
				qryUecOutStaffInfo.setPsnCode(uecOutStaffInfoVo.getStaffCode());
				qryUecOutStaffInfo = uecOutStaffManager.findUecOutStaffInfo(qryUecOutStaffInfo);
				if (null != qryUecOutStaffInfo) {
					Events.postEvent("onOK", this.self, qryUecOutStaffInfo);
					this.onCancel();
				}
			}
		}
	}

	/**
	 * bindBean
	 */
	public void bindBean() {
		opType = (String) arg.get("opType");
		if ("outStaffList".equals(opType)) {
			this.bean.getUecOutStaffListMainWin().setTitle("员工选择列表");
			uecOutStaffInfoVo = (UecOutStaffInfoVo) arg.get("uecOutStaffInfoVo");
			if (null != uecOutStaffInfoVo) {
				this.onQueryUecOutStaffInfoVo();
			}
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		this.bean.getUecOutStaffListMainWin().onClose();
	}

	/**
	 * 查询--uecOutStaffInfoVo
	 */
	public void onQueryUecOutStaffInfoVo() {
		if (null != uecOutStaffInfoVo) {
			int activePage = bean.getUecOutStaffListboxPaging().getActivePage();
			int pageSize = bean.getUecOutStaffListboxPaging().getPageSize();
			PageInfo pageInfo = uecOutStaffManager.queryPageInfoByUecOutStaffInfoVo(uecOutStaffInfoVo, activePage + 1,
					pageSize);
			if(pageInfo != null) {
				ListModelList dataList = new BindingListModelList(pageInfo.getDataList(), true);
				bean.getUecOutStaffListbox().setModel(dataList);
				bean.getUecOutStaffListboxPaging().setTotalSize(NumericUtil.nullToZero(pageInfo.getTotalCount()));
			}else {
				bean.getUecOutStaffListboxPaging().setTotalSize(NumericUtil.nullToZero(0));

			}
		}
	}

	/**
	 * 翻页
	 */
	public void onUecOutStaffListboxPaging() {
		if (null != uecOutStaffInfoVo) {
			this.onQueryUecOutStaffInfoVo();
		}
	}
}
